package com.fmjava.controller;


import com.fmjava.ServiceImpl.UserServiceImpl;
import com.fmjava.domain.User;
import com.fmjava.service.UserService;
import com.sun.javafx.collections.MappingChange;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");//设置编码
       String code=request.getParameter("code");
        HttpSession session = request.getSession();
        String codes = (String) session.getAttribute("checkcode_session");
        if (codes == null||code==null) {
            request.setAttribute("codeInfo","验证码不能为空");
            request.getRequestDispatcher("/regist.jsp").forward(request,response);

        }
        if (!codes .equals(code)) {
            request.setAttribute("codeInfo","验证码错误");
            request.getRequestDispatcher("/regist.jsp").forward(request,response);
        }
       User user=new User();
        Map<String,String[]> maps=request.getParameterMap();
        try {
            BeanUtils.populate(user,maps);

        }catch (Exception e) {
            e.printStackTrace();
        }
        UserService userservice=new UserServiceImpl();//面向接口编程
        System.out.println(111);
        System.out.println(user.toString());
        response.setContentType("text/html;charset=utf-8");
        int result=userservice.addUser(user);
        System.out.println(result);
        if (result == 1) {
            request.getRequestDispatcher("/admin_login.jsp").forward(request,response);
        }
    }
}
